package com.example.music.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.dto.SingerDto;
import com.example.music.entity.Singer;
import com.example.music.service.SingerService;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ResponseMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("singer")
public class SingerController {
    @Autowired
    SingerService singerService;

    /**
     * 添加歌手
     *
     * @param singer
     * @return
     */
    @PostMapping("add")
    public AjaxJson add(@RequestBody Singer singer) {
        //配置时间注解
//        Singer singer = new Singer();
//        BeanUtil.copyProperties(singerDto,singer, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        String birth = singerDto.getBirth();
//        singer.setBirth(DateUtil.parse(birth));
        singerService.add(singer);
        return AjaxJson.getSuccess(null);
    }

    @PostMapping("update")
    public AjaxJson update(@RequestBody Singer singer){
        singerService.update(singer);
        return AjaxJson.getSuccess(null);
    }

    /**
     * 通过id删除歌手
     *
     * @param ids
     * @return
     */
    @GetMapping("delete")
    public AjaxJson delete(@RequestParam("ids") String ids) {
        singerService.deleteByIds(ids);
        return AjaxJson.getSuccess(null);
    }

    /**
     * 查找所有歌手
     *
     * @return
     */
    @GetMapping("allSinger")
    public AjaxJson allSinger() {
        return AjaxJson.getSuccessData(singerService.selectAll());
    }

    /**
     * 通过名字模糊查找歌手
     *
     * @param name
     * @return
     */
    @GetMapping("singerByName")
    public AjaxJson singerByName(@RequestParam String name) {
        List<Singer> singers = singerService.findSingerByName(name);
        if (singers.size() > 0) {
            return AjaxJson.getSuccessData(singers);
        } else {
            return AjaxJson.getError("无法查询到歌手");
        }
    }

    /**
     * 通过性别查找歌手
     *
     * @param sex
     * @return
     */
    @GetMapping("singerBySex")
    public AjaxJson singerBySex(@RequestParam String sex) {
        List<Singer> singers = singerService.findSingerBySex(sex);
        if (singers.size() > 0) {
            return AjaxJson.getSuccessData(singers);
        } else {
            return AjaxJson.getError("无法查询到歌手");
        }
    }

    @PostMapping("singerById")
    public AjaxJson singerById(@RequestParam String id){
        Singer singer = singerService.selectItemById(id);
        if (singer != null){
            return AjaxJson.getSuccessData(singer);
        }else {
            return AjaxJson.getError("请输入正确的歌手id");
        }
    }

    /**
     * 通过id更改头像
     *
     * @param id
     * @param pic
     * @return
     */
    @PostMapping("updateSingerPic")
    public AjaxJson updateSingerPic(@RequestParam Integer id, @RequestParam("file") MultipartFile pic) {
        return singerService.updateSingerPic(id,pic);
    }

    @PostMapping("sex")
    public AjaxJson sex() {
        return singerService.sex();
    }

    @PostMapping("singerByCountry")
    public AjaxJson singerByCountry() {
        return singerService.singerByCountry();
    }
}
